home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows News 2010 Summer - Disc 1
/
WN_Ete2010_CD1.iso
/
Onglet5
/
Weezo
/
Weezo setup.exe
/
{code_appDir}
/
www
/
local
/
wowConfig.php
< prev
Wrap
PHP Script
|
2010-05-19
|
20KB
|
591 lines
<?php
/**
* Wake on Wan configuration script
*
* config format: $wowEnabled:$useHost:mackPack($mac):port:[$host]
* 0/1 $wowEnabled: is wake on wan enabled
* 0/1 $useHost: 1 to use hostname instead of last known IP
* string(8) mackPack($mac): packed MAC
* string(3) port: base64 encoded 16bits port
* string(0-40) host (conditional): host name
*
*
* PHP version 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category NA
* @package NA
* @author Nicolas Bruley / Peer 2 World <contact@weezo.net>
* @copyright 2005-2008 Nicolas Bruley / Peer 2 World
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id:$
* @link http://www.weezo.net
* @since File available since Release 1.1.2
*/
require(INCLUDE_DIR.'outputFunctions.php');
$_ENV['configurationEnvironment']='application';
define('HOST_MAX_LENGTH',30);
/**
* @desc Check, pack and b64 encode MAC address
*
* @param string $mac: MAC address formated like: XX-XX-XX-XX-XX-XX or XX:XX:XX:XX:XX:XX
* @return string packed mack or false if format is incorrect
*/
function macPack($mac){
// Check MAC format
if(preg_match("/^([0-9a-fA-F]{1,2}-){5}[0-9a-fA-F]{1,2}$/",$mac)) $mac=explode('-',$mac);
elseif(preg_match("/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/",$mac)) $mac=explode(':',$mac);
else return false;
$macPack=''; foreach ($mac as $m) $macPack.=chr(hexdec($m));
return base64_encode($macPack);
}
/**
* @desc b64 decode and Unpack MAC address (see above)
*
* @param string $packed
* @return string
*/
function macUnpack($packed){
if(strlen($packed=base64_decode($packed))!=6) return false;
$unpacked=((ord($packed[0])<16)?'0':'').dechex(ord($packed[0]));
for($i=1;$i<6;$i++) $unpacked.='-'.((ord($packed[$i])<16)?'0':'').dechex(ord($packed[$i]));
return strtoupper($unpacked);
}
/**
* @desc Check if host format is ok
*
* @param string $host
* @return boolean
*/
function checkHost($host){
if(preg_match("/^[a-zA-Z0-9\.-]{4,".HOST_MAX_LENGTH."}$/",$host) && strpos($host,'.')>0 && strrpos($host,'.')<strlen($host)-2) return true;
return false;
}
/**
* @desc Shutdown UDP listening socket
*
*/
function shutdownSocket(){
global $serverSock;
@socket_close($serverSock);
//cfDbg('Socket shutdown',1);
}
// Don't know how it did ever work, but everything is coded with GET whereas sendData (may) use POST
if(count($_POST)) $_GET=$_POST;
/*
***************************************************************************************************************************
* Parse network adapters
***************************************************************************************************************************
*/
$adapters=array();
if(isset($_GET['adapters']) && $_GET['adapters']) foreach (explode('/',base64_decode($_GET['adapters'])) as $adapter){
if(count(explode(':',$adapter))==3)
@list($mac,$name,$traffic)=explode(':',$adapter);
elseif(count(explode(':',$adapter))==2){
@list($mac,$name)=explode(':',$adapter);
$traffic=0;
}
else
continue;
$mac=macUnpack(macPack($mac));
if(!$mac) continue;
// If adapter already defined
if(isset($adapters[$mac])){
if(strlen($name)<strlen($adapters[$mac]['name'])) $adapters[$mac]['name']=$name;
$adapters[$mac]['traffic']+=$traffic;
}
else $adapters[$mac]=array('name'=>$name,'traffic'=>$traffic);
// Lower priority of VMWare adapters
if(stripos($adapters[$mac]['name'],'vmware')!==false) $adapters[$mac]['traffic']=0;
}
/*
***************************************************************************************************************************
* Parse configuration
***************************************************************************************************************************
*/
if(isset($_GET['config']) && strlen($_GET['config'])>=18 && count(explode(':',$_GET['config']))==5){
list($wowEnabled,$wowUseHost,$wowMac,$wowPort,$wowHost)=explode(':',$_GET['config']);
$wowMac=macUnpack($wowMac);
$wowPort=unpack('S',base64_decode($wowPort));$wowPort=$wowPort[1];
}
else{
$wowEnabled=((isset($_GET['config']) && $_GET['config']=='1:')?1:0); // Set default enabled if user clicked on enable WOW with empty config
$wowUseHost=0;
$wowMac=' - - - - - ';
// Select default network adapter (use the one with most traffic)
$maxTraffic=-1;
foreach ($adapters as $mac=>$adapter) if(@$adapter['traffic']>$maxTraffic){
$maxTraffic=$adapter['traffic'];
$wowMac=$mac;
}
$wowPort=cfGGetVar('serverPort');
$wowHost='';
}
/*
***************************************************************************************************************************
* Process GET commands
***************************************************************************************************************************
*/
if(isset($_GET['wowForm'])){
$configOK=true;
// WOW enabled
$wowEnabled=((isset($_GET['wowEnabled']))?'1':'0');
$config=$wowEnabled.':';
// Use host name instead of IP
$config.=($wowUseHost=(((isset($_GET['wowUseHost']) && $_GET['wowUseHost']=='true'))?'1':'0')).':';
// MAC
if(!isset($_GET['wowAdapter']) || $_GET['wowAdapter']=='other') {
if(isset($_GET['wowMac0'])){
$wowMac=$_GET['wowMac0'];
for($i=1;$i<6;$i++) $wowMac.='-'.$_GET['wowMac'.$i];
}
else $wowMac=' - - - - - ';
}
else $wowMac=$_GET['wowAdapter'];
if(macPack($wowMac)) $config.=macPack($wowMac).':'; else $configOK=false;
// Port
if(isset($_GET['wowPort'])){
$wowPort=$_GET['wowPort'];
$config.=base64_encode(pack('S',$wowPort)).':';
}
else $wowPort='';
// Host name
if(isset($_GET['wowHost']) && $_GET['wowHost']){
if(checkHost($_GET['wowHost'])) {
$wowHost=strtolower($_GET['wowHost']);
$config.=$wowHost;
}
else $configOK=false;
}
// Full test
$wowFullTest =(isset($_GET['testFull']) && $_GET['testFull']);
/**
* Async Request: perform WOW simple test
*/
if(cfIsAsync()){
echo cfAsyncHeader();
// If using hostname, first resolve
if($wowUseHost){
$ip=@gethostbyname($wowHost);
// If hostname couldn't be resolved
if(!$ip || $ip==$wowHost){
echo cfAsyncXMLJSaction('dgi("wowHost").style.color="red"');
echo cfAsyncXMLJSaction('setTestResult("'.cfCaption('wowUnknownHost').'",1,"'.outIcon('warning').'")');
die(cfAsyncFooter());
}
// If resolved hostname ip doesn't match with client IP
if(isset($_GET['externalIP']) && $ip!=$_GET['externalIP']){
echo cfAsyncXMLJSaction('dgi("wowHost").style.color="red"');
echo cfAsyncXMLJSaction('setTestResult("'.cfCaption('wowHostNotMatchingIP').'",1,"'.outIcon('warning').'")');
die(cfAsyncFooter());
}
}
// Open local UDP listening socket
if(!($serverSock = @socket_create(AF_INET,SOCK_DGRAM,SOL_UDP)) || (!@socket_bind($serverSock, 0, $wowPort))) {
echo cfAsyncXMLJSaction('wowTestSocketError("Socket error, UDP port '.$wowPort.' seems to be used")');
@socket_close($serverSock);
die(cfAsyncFooter());
}
socket_set_nonblock($serverSock);
register_shutdown_function('shutdownSocket');
// Ask weezo server a magic packet
$serverResult=cfSocketHTTPRequest('http://weezo.net/wow.php?api=1&mac='.$wowMac.'&port='.$wowPort);
// Problem sendign UDP packet
if($serverResult!='OK') {
echo cfAsyncXMLJSaction('setTestResult("'.cfCaption('serverError').'",1)');
die(cfAsyncFooter());
}
// wait for incoming packet
// Wait for UDP packet...
while($i<40){ // 3s
//echo $i++.(($i%10==0)?'<br>':'.'); flush();
if($src = @socket_recv($serverSock, $data, 200, 0)) {
for($o=0;$o<6;$o++) if(ord($data[$o])!=255) break;
if($o==6) {
echo cfAsyncXMLJSaction('wowTestSuccess()');
echo cfAsyncFooter();
exit;
}
}
usleep(100000);
$i++;
}
echo cfAsyncXMLJSaction('wowTestFailure()');
die(cfAsyncFooter());
}
/**
* Close window and send config to application (prepend a "$:" if a full test must be performed
*/
if($configOK||!$wowEnabled) {
cfInsertHEAD();
//$config=str_replace('+','*weezoPlus*',$config);
die('<body onload="wl.UICommand(\'close\',\''.(($wowFullTest)?'$:':'').$config.'\')"></body>');
}
}
/*
***************************************************************************************************************************
* Insert HEAD, JS functions and form
***************************************************************************************************************************
*/
cfInsertHEAD(false);
echo cfScriptLink('winClient.js');
?>
<meta width="490x360"></meta>
<meta title="<?php echo cfCaption('genConfigure');?>"></meta>
<script type="text/javascript">
// Check all fields
function checkAll(){
// WOW enabled
dgn("wowEnabled").checked=true;
if(dgn("wowEnabled").checked) dis='';
// WOW disabled
else dis='disabled';
dgn("wowUseHost",0).disabled=dis;
dgn("wowUseHost",1).disabled=dis;
dgn("wowHost").disabled=dis;
dgn("wowPort").disabled=dis;
dgn("wowAdapter").disabled=dis;
for(var i=0;i<6;i++) dgn('wowMac'+i).disabled=dis;
// Enable/disable controls, set error fields
macOK(); portOK(); hostOK();
if(dgn("wowEnabled").checked && (!macOK() || !portOK() || !hostOK())){
wl.button.disable('testBt');
wl.button.disable('testFullBt');
wl.button.disable('validateBt');
return 0;
}
if(dgn("wowEnabled").checked) wl.button.enable('testBt'); else wl.button.disable('testBt');
if(dgn("wowEnabled").checked) wl.button.enable('testFullBt'); else wl.button.disable('testFullBt');
wl.button.enable('validateBt');
return 1;
}
// Click on validate button
function validate(){
if(!checkAll()) return;
dgn('wowHost').enabled='enabled';
D.wowForm.submit();
}
// Check MAC
function macOK(){
var macOK=true;
for(var i=0;i<6;i++) {
dgn('wowMac'+i).disabled=((dgn('wowAdapter').value=='other' && dgn("wowEnabled").checked)?'':'disabled');
if(dgn('wowMac'+i).disabled=='' && !dgn('wowMac'+i).value.match(/^[0-9a-fA-F]{2}$/)){
dgn('wowMac'+i).style.color='red';
dgn('wowMac'+i).style.backgroundColor='#FEE';
macOK=false;
}
else{
dgn('wowMac'+i).style.color='';
dgn('wowMac'+i).style.backgroundColor='';
}
}
return macOK;
}
// Check port
function portOK(){
if(dgn('wowPort').value.match(/^[0-9]{1,5}$/) && dgn('wowPort').value<65536 && dgn('wowPort').value>0){
dgn('wowPort').style.color='';
dgn('wowPort').style.background='';
return true;
}
dgn('wowPort').style.color='red';
dgn('wowPort').style.background='#FEE';
return false;
}
// Check host
function hostOK(){
if(dgn('wowUseHost',0).checked) dgn('wowHost').disabled='disabled';
else dgn('wowHost').disabled='';
if(dgn('wowHost').value.match(/^[a-zA-Z0-9\.-]{1,<?php echo HOST_MAX_LENGTH; ?>}$/) && dgn('wowHost').value.indexOf('.')>0 && dgn('wowHost').value.lastIndexOf('.')< dgn('wowHost').value.length-2){
dgn('wowHost').style.color='';
dgn('wowHost').style.backgroundColor='';
return true;
}
if(dgn('wowUseHost',1).checked) {
dgn('wowHost').style.color='red';
dgn('wowHost').style.backgroundColor='#FEE';
}
else{
dgn('wowHost').style.color='';
dgn('wowHost').style.backgroundColor='';
}
return dgn('wowUseHost',0).checked;
}
// Enable/disable WOW
function wowEnabledChanged(){checkAll();}
// Select an adapter
function adapterChanged(){
if(dgn('wowAdapter').value!='other'){
var parts=dgn('wowAdapter').value.split('-');
for(var i=0;i<parts.length;i++) dgn('wowMac'+i).value=parts[i];
}
checkAll();
}
// Change MAC
function macChanged(){checkAll();}
// Display help page
var helpDisplayed=0;
function help(){
helpDisplayed=1-helpDisplayed;
dgi('helpFrameTxt').innerHTML=dgi('helpFrameTemplate').innerHTML.replace(/PORT/,dgi('wowPort').value).replace(/<a/gi,'<a class="link"');
dgi('helpFrame').style.display=(helpDisplayed)?'':'none';
dgi('configFrame').style.display=(helpDisplayed)?'none':'';
}
// Simple WOW test
var wowTestRun=0;//IE animated gif workaround
function wowTest(){
if(!checkAll()) return;
if(!wowTestRun) {dgi('testFrame').style.display='';wowTestRun=1;dgi('testFrame').style.display='none'}
maskShow(false,true);
fade(dgi('testFrame'),0,1);
maskMoveAbove(dgi('testFrame'))
dgi('wowTestLoadingIcon').style.display='';
dgi('wowTestResultIcon').style.display='none';
dgi('testResult').innerHTML='<?php echo cfCaption('accessTesting');?>';
dgi('testResult').className='';
asyncSubmitForm("wowForm");
}
function setTestResult(text,isError,iconSrc){
dgi('wowTestLoadingIcon').style.display='none';
dgi('testResult').innerHTML=text;
dgi('testResult').className=(isError)?'warning':'';
if(iconSrc) {
dgi('wowTestResultIcon').style.display='';
dgi('wowTestResultIcon').src=iconSrc;
}
}
function hideTestFrame(){maskHide(1);fade(dgi('testFrame'),1,0);}
function wowTestSocketError(errStr){setTestResult(errStr,1,"<?php echo outIcon('warning');?>");}
function wowTestSuccess(){setTestResult('<?php echo '<b style="color:#273">'.cfCaption('genOK').'</b>'?>',0,"<?php echo outIcon('ok');?>");}
function wowTestFailure(){setTestResult('<?php echo cfCaption('transfersFailed');?>',1,"<?php echo outIcon('warning');?>");}
// Full WOW test
function wowTestFull(){
if(!checkAll()) return;
dgn('wowAdapter').style.visibility='hidden';
maskShow(false,true);
maskMoveAbove(dgi('testFullFrame'));
fade(dgi('testFullFrame'),0,1);
}
function hideTestFullFrame(){maskHide(1);fade(dgi('testFullFrame'),1,0);dgn('wowAdapter').style.visibility='visible';}
function fullTestRun(){
hideTestFullFrame();
wl.button.setIcon('validateBt','<?php echo outIcon('wowTestFull')?>');
dgn('testFull').value=1;
}
function fullTestCancel(){
hideTestFullFrame();
wl.button.setIcon('validateBt','<?php echo outIcon('ok')?>');
dgn('testFull').value=0;
}
</script>
</head>
<body style="overflow:hidden">
<?php
/**
* Enable/Disable Wake on Wan
*/
echo '<form name="wowForm" id="wowForm" method="GET" enctype="multipart/form-data">';
echo outDivFrame('frame1');
echo outFrameHeaderTable("frame1Header",cfCaption('accountWOW'),'<div style="display:none"><input name="wowEnabled" type="checkbox" '.(($wowEnabled)?'checked="checked"':'').' onclick="wowEnabledChanged()"> <span onclick="dgn(\'wowEnabled\').checked=((dgn(\'wowEnabled\').checked)?\'\':\'checked\');wowEnabledChanged()" style="cursor:default">'.cfCaption('genActivated').'</span> </div>'.outButton(cfCaption('genHelp'),'javascript:help()',outIcon('help')));
?>
<br>
<!-- Hidden fields -->
<input name="wowForm" type="text" value="1" style="display:none">
<input name="adapters" type="text" value="<?php echo str_replace('"','',$_GET['adapters']);?>" style="display:none">
<input name="config" type="text" value="<?php echo str_replace('"','',(isset($_GET['config']))?$_GET['config']:'');?>" style="display:none">
<input name="externalIP" type="text" value="<?php echo $_GET['externalIP'];?>" style="display:none">
<input name="testFull" type="text" value="0" style="display:none">
<?php
/**
* Help frame
*/
echo '<div id="helpFrame" style="display:none">'.outDivFrame('frame2').'<div class="frame2Header">'.cfCaption('genHelp').'</div><div id="helpFrameTemplate" style="display:none">'.cfCaption('wowHelp','<b>'.'PORT'.'</b>','<b>'.((cfGGetVar('LANIPForced'))?cfGGetVar('LANIPForced'):cfGGetVar('LANIPComputed')).'</b>').'</div>';
echo '<div id="helpFrameTxt"></div><br>';
echo '<center>'.outButton(cfCaption('genClose'),'javascript:help()',outIcon('close')).'</center></div></div>';
echo '<div id="configFrame">';
/**
* Host / IP
*/
echo outDivFrame('frame2').'<div class="frame2Header">'.cfCaption('contactURL').'</div>';
?>
<span onclick="checkAll()" style="cursor:default"><input type="radio" name="wowUseHost" value="false" <?php echo (($wowUseHost)?'':'checked="checked"');?>>
<span onclick="dgn('wowUseHost',0).checked='checked'"><?php echo cfCaption('configWOWUseIP'); ?></span></span>
<br>
<span onclick="checkAll()" style="cursor:default"><input type="radio" name="wowUseHost" value="true" <?php echo ((!$wowUseHost)?'':'checked="checked"');?>>
<span onclick="dgn('wowUseHost',1).checked='checked'"><?php echo cfCaption('configWOWUseHost'); ?></span></span><br>
<input name="wowHost" type="text" value="<?php echo $wowHost; ?>" size="30" maxlength="<?php echo HOST_MAX_LENGTH;?>" onkeyup="checkAll()" style="margin-left:2em">
<br>
<?php
/**
* Port
*/
?>
<span style="margin-right:2em;cursor:default;font-weight:bold"><?php echo cfCaption('genPort');?> (UDP)</span><input name="wowPort" type="text" size="1" value="<?php echo $wowPort;?>" onkeyup="checkAll()" maxlength="5">
</div>
<br>
<?php
/**
* Network adapters list
*/
echo outDivFrame('frame2').'<div class="frame2Header">'.cfCaption('configWOWAdapters').'</div>';
echo '<select name="wowAdapter" onchange="adapterChanged()" style="width:400px">';
$other=true;
foreach ($adapters as $mac=>$adapter){
if($mac==$wowMac) $other=false;
echo '<option value="'.$mac.'" '.(($mac==$wowMac)?'selected':'').'>'.cfUTF8Encode($adapter['name']).'</option>';
}
echo '<option value="other" '.(($other)?'selected':'').'>'.cfCaption('userNewIcon').'</option>';
?>
</select>
<br><b>MAC </b>
<?php
/**
* MAC input
*/
$mac=explode('-',$wowMac);
for($i=0;$i<6;$i++) echo '<input type="text" name="wowMac'.$i.'" value="'.trim($mac[$i]).'" maxlength="2" style="width:2em;text-align:center" '.(($other)?'':'disabled="disabled"').' onchange="macChanged()" onkeyup="macChanged()">'.(($i<5)?' - ':'');
?>
</div>
<br>
<center>
<?php
/**
* Buttons
*/
// Config ok ?
if(!$wowEnabled || ($wowMac!=' - - - - - ' && (!$wowUseHost || checkHost($wowHost)))) $state='up'; else $state='off';
// Test
echo outDivFrame('frame2');
echo outButton(cfCaption('genTest'),'javascript:void(wowTest())',outIcon('wowTest'),false,'testBt');
echo ' ';
// Full test
echo outButton(cfCaption('genTestFull').'...','javascript:void(wowTestFull())',outIcon('wowTestFull'),false,'testFullBt');
echo '</div><br>';
// Save/apply
echo outButton(cfCaption('genValidate'),'javascript:validate()',outIcon('ok'),false,'validateBt');
echo ' ';
// Cancel
echo outButton(cfCaption('genCancel'),'javascript:wl.UICommand(\'close\')',outIcon('cancel'),false,'cancelBt');
?>
<br>
</center>
</div>
</div>
<div id="maskDiv" style="position:absolute;top:0;left:0;width:100%;height:100%;background:black;display:none;z-index:1000"></div>
<?php
/**
* WOW Test frame
*/
?>
<div id="testFrame" style="position:absolute;top:30%;left:50%;width:50%;display:none;z-index:1001">
<div class="popup" style="position:relative;width:300px;left:-150px;margin-right:-150px">
<div class="popupHeader"><?php echo cfCaption('genTest');?></div>
<br>
<center>
<?php echo outImage(outIcon('loading'),false,'id="wowTestLoadingIcon"','vertical-align:middle');?>
<?php echo outImage(outIcon('loading'),false,'id="wowTestResultIcon"','vertical-align:middle;display:none');?>
<span id="testResult" style="margin-left:2em;margin-right:2em"></span><br><br><br>
<?php echo outButton(cfCaption('genClose'),'javascript:hideTestFrame()',outIcon('close')); ?>
</center>
<br>
</div>
</div>
<?php
/**
* WOW Full Test frame
*/
?>
<div id="testFullFrame" style="position:absolute;top:10%;left:50%;width:50%;display:none;z-index:1001">
<div class="popup" style="position:relative;width:300px;left:-150px;margin-right:-150px">
<div class="popupHeader"><?php echo cfCaption('genTestFull');?></div>
<br><div style="text-align:justify">
<?php
$icon=outImage(outIcon('warning'),false,false,'float:left:padding-bottom:2em;margin-right:1em');
echo str_replace('<b>',$icon.'<b>',cfCaption('wowTestFullText'));
?><br clear="all">
<br>
<br>
<center>
<?php echo outButton(cfCaption('genOK'),'javascript:fullTestRun()',outIcon('wowTestFull')); ?>
<?php echo outButton(cfCaption('genCancel'),'javascript:fullTestCancel()',outIcon('cancel')); ?>
</center>
<br>
</div>
</div>
</div>
<script type="text/javascript">checkAll()</script>
</form>
</body>